package com.kami.ip;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class bmi extends AppCompatActivity {

    String[] feetArray, inchesArray;
    int feet, inches;
    EditText vWeight;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         //调用父类oncreate方法
        setContentView(R.layout.activity_bmi);      //填充activity_bmi布局到页面
        vWeight = findViewById(R.id.weight);        //找到weight的编辑框
        feetArray = getResources().getStringArray(R.array.feet);    //将strings.xml中的8个item赋给feetArrty字符串数组
        inchesArray = getResources().getStringArray(R.array.inches);//将strings.xml中的12个item赋给inchesArrty字符串数组
        Spinner spinnerFeet =  findViewById(R.id.spinner_feet);         //找到定义在activity_bmi布局中feet spaneer,类似于一个存放下拉框的框
        ArrayAdapter<String> adapterFeet = new ArrayAdapter<>(this, R.layout.dropdown_item, feetArray);
        //为feetArray创建适配器，
        // @param context The current context.
        // * @param resource The resource ID for a layout file containing a TextView to use when
        // * instantiating views.
        // * @param objects The objects to represent in the ListView
        spinnerFeet.setAdapter(adapterFeet);        //为Feet设置刚刚创建好的适配器
        Spinner spinnerInches = findViewById(R.id.spinner_inches);          //feet设置适配器的步骤类似
        ArrayAdapter<String> adapterInches = new ArrayAdapter<>(this, R.layout.dropdown_item, inchesArray);
        spinnerInches.setAdapter(adapterInches);

        //为编辑框spinnerFeet设置列表选择监听器，与button按钮的监听器类似
        spinnerFeet.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                feet = arg0.getSelectedItemPosition() + 1;  //feet要填充的数据的下表就是点击的位置下标+1，因为strings中设置的item下标是从1开始
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                //如果没选择任何一项，则不执行任何代码
            }
        });
        //为编辑框spinnerInches设置列表选择监听器，与button按钮的监听器类似,与上面的步骤类似
        spinnerInches.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                inches = arg0.getSelectedItemPosition();   //此处下标不+1是因为strings资源文件中inches的item的下表是从0开始的，和屏幕点击到的位置下表一致
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        //监听提交按钮，并执行计算BMi函数
        findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calcBMI(v);
            }
        });
        registerForContextMenu(findViewById(R.id.imageViewBmi)); //注册长按菜单显示
        findViewById(R.id.about).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openOptionsDialog();  //弹窗一个dialog,方法体定义在下面
            }
        });

    }

    //计算BMi的值
    public void calcBMI(View view) {
        String weight = vWeight.getText().toString();//将体重编辑框得到数据转为string类型
        Intent intent = new Intent(this, reportActivity.class);  //设置显示intent意图
        Bundle bundle = new Bundle();                           //创建一个新的bundle对象
        /**
         * Constructs a Bundle whose data is stored as a Parcel.  The data
         * will be unparcelled on first contact, using the assigned ClassLoader.
         *
         * @param parcelledData a Parcel containing a Bundle
         *
         * @hide
         */
        //判断下拉框以及体重编辑框的内容不能为空，三者是或的关系
        if ( feetArray.equals("")|| weight.equals("")||inchesArray.equals(""))
        {
            Toast.makeText(this, R.string.bmi_warning,
                    Toast.LENGTH_SHORT).show();
        }else {
            bundle.putInt("feet", feet);   //bundle绑定feet信息
            bundle.putInt("inches", inches);//bundle绑定inches信息
            bundle.putString("weight", weight); //bundle绑定体重信息
            intent.putExtras(bundle);       //将bunle对象赋给intent对象，bundle是携带数据的对象，intent是在两个activity之间传递数据的对象
            startActivity(intent);      //启动intent
        }

    }

    // --- Option Menu ---
    //右上角菜单选项
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_options, menu);//将menu_option赋值映射到menu上，
        return true;
    }
    //为其设置选项点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_about:
                openOptionsDialog();  //弹窗一个dialog,方法体定义在下面
                return true;
            case R.id.menu_wiki:
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://en.wikipedia.org/wiki/Body_mass_index"));
                startActivity(intent);
                return true;
            case R.id.menu_exit:
                finish();
                return true;
        }
        return false;
    }

    //弹出一个dialog,只要掉用这个方法就会弹出一个设计好的dialog
    public void openOptionsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(bmi.this);    //创建一个dialog  /**多选对话框*/
        builder.setTitle(R.string.about_button);                    //设置标题
        builder.setMessage(R.string.about_msg);             //填充信息，信息体在string中，这样做的好处是可以通过修改string的内容，来适配系统语言
        builder.setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    { }
                });
        builder.create();
        builder.show();
    }

    //context
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_context, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.bmiWiki:
                //填写bmi维基解释
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://en.wikipedia.org/wiki/Body_mass_index"));
                startActivity(intent);
                return true;
            case R.id.aboutBmi:
                openOptionsDialog();
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }


}
